package com.xdxc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xdxc.model.dto.QuestionForm;
import com.xdxc.model.dto.QuestionTypeForm;
import com.xdxc.model.entity.Question;
import com.xdxc.response.Result;
import com.xdxc.service.QuestionService;
import com.xdxc.service.impl.QuestionsService;
import com.xdxc.vo.QuestionWithOptionsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/question-service")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired

    private QuestionsService questionsService;

    @PostMapping("/qu-idList")
    public Result getQuestionBatchByIdList(@RequestBody List<Long> idList){
       List<QuestionWithOptionsVo> questionList= questionService.getQuestionBatchByIdListWithCorrectAnswer(idList);
        return Result.success(questionList);
    }
    @GetMapping("/question/{quId}")
    public Result getQuestionWithOptionById(@PathVariable Long quId){
        QuestionWithOptionsVo question= questionService.getQuestionWithOptionById(quId,false);
        return Result.success(question) ;
    }
    @GetMapping("/questionCorrect/{quId}")
    public Result getQuestionWithCorrectById(@PathVariable Long quId){
        QuestionWithOptionsVo question= questionService.getQuestionByIdWithCorrectAnswer(quId);
        return Result.success(question) ;
    }
    @GetMapping("/questionlist")
    public Result list(){

        return null;
    }


    @PostMapping("/qu-type")
    public Result getQuestionWithOptionByType(@RequestBody QuestionTypeForm questionTypeForm){
        Page<Question> questionPage = new Page<>(questionTypeForm.getPage(), questionTypeForm.getPageSize());

        // 创建查询条件构造器
        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
        // 增加查询条件（根据实际需求调整）
        queryWrapper.eq(Question::getQuType, questionTypeForm.getQuType());

        // 使用 MyBatis-Plus 的分页查询，传入查询条件
        IPage<Question> pagedQuestions = questionService.page(questionPage, queryWrapper);
        List<Long> idList = pagedQuestions.getRecords().stream().map(Question::getId).collect(Collectors.toList());
        List<QuestionWithOptionsVo> questionList= questionService.getQuestionBatchByIdListWithCorrectAnswer(idList);
        Result result=  Result.success(questionList);
        result.add("totalPage",pagedQuestions.getTotal());
        result.add("currentPage",pagedQuestions.getCurrent());
        result.add("pageSize",pagedQuestions.getSize());
        return result;
    }



    @GetMapping("/question-list")
    public Result getQuestionByIdWithCorrectAnswer(
            @RequestParam(defaultValue = "1") int page, // 页码，默认为1
            @RequestParam(defaultValue = "5") int size  // 每页条数，默认为5
    ) {
        return  questionService.questionPage(page, size);
    }
    @PostMapping("/add-question")
    public Result saveQuestion(@RequestBody @Valid QuestionForm formData ) {
        boolean result = questionsService.addQuestion(formData);
        if (!result){
            return Result.error("出错了");
        }
        return Result.success("添加试题成功").setMsg("添加试题成功");
    }





}
